/**
 * Created by Administrator on 2017/2/8.
 */

var curpage = 1;
var page = pagesize;
var hasMore = true;
var footer = false;
var reset = true;
var levstr = '一';
var Lev = 1;
$(function () {
    var key = getCookie('key');
    if (!key) {
        window.location.href = WapSiteUrl + '/tmpl/member/login.html';
    }
    $("#lev1").click(function(){
        tabContent($(this),1);
    });
    $("#lev2").click(function(){
        tabContent($(this),2);
    });
    $("#lev3").click(function(){
        tabContent($(this),3);
    });

    $('.top p').on('touchstart', function () {
        $(this).addClass('active').siblings().removeClass('active')
    })

    //滚动header固定到顶部
    //$.scrollTransparent();

    function tabContent(option,lev) {
        Lev = lev;
        var tabA = document.getElementsByClassName('top')[0].getElementsByTagName('p');
        for (var i = 0; i < tabA.length; i++) {
            tabA[i].className = '';
        }
        switch (Lev) {
            case 1:
                levstr = "一"
                break
            case 2:
                levstr = "二"
                break
            case 3:
                levstr = "三"
                break
        }
        // option.className = 'active';
        option.addClass('active');
        hasMore = true;
        curpage = 1;
        $("#content").empty();
        initPage();
    }

    function initPage() {
        if (reset) {
            curpage = 1;
            hasMore = true;
        }
        $('.loading').remove();
        if (!hasMore) {
            return false;
        }
        hasMore = false;
        $.ajax({
            type: 'post',
            url: ApiUrl + "/index.php?act=fenxiao&op=member_list&page=" + page + "&curpage=" + curpage,
            data: {key: key, lev: Lev},
            dataType: 'json',
            success: function (result) {
                curpage++;
                hasMore = result.hasmore;
                if (!hasMore) {
                    get_footer();
                }
                if (result.datas.member_list.length <= 0) {
                    $('#footer').addClass('posa');
                } else {
                    $('#footer').removeClass('posa');
                }
                var data = result;
                data.WapSiteUrl = WapSiteUrl;//页面地址
                data.ApiUrl = ApiUrl;
                data.key = getCookie('key');
                data.levstr = levstr;
                template.helper('$getLocalTime', function (nS) {
                    var d = new Date(parseInt(nS) * 1000);
                    var s = '';
                    s += d.getFullYear() + '年';
                    s += (d.getMonth() + 1) + '月';
                    s += d.getDate() + '日 ';
                    s += d.getHours() + ':';
                    s += d.getMinutes();
                    return s;
                });
                template.helper('p2f', function (s) {
                    return (parseFloat(s) || 0).toFixed(2);
                });
                template.helper('parseInt', function (s) {
                    return parseInt(s);
                });
                var html = template.render('content-tmpl', data);
                if (reset) {
                    reset = false;
                    $("#content").html(html);
                } else {
                    $("#content").append(html);
                }
            }
        });
    }

//初始化页面
    initPage();
    $(window).scroll(function () {
        if (($(window).scrollTop() + $(window).height() > $(document).height() - 1)) {
            initPage();
        }
    });
});
function get_footer() {
    if (!footer) {
        footer = true;
        $.ajax({
            url: WapSiteUrl + '/js/tmpl/footer.js',
            dataType: "script"
        });
    }
}